package com.jiangxiaofei.citycloud.activity.freshfruits;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.blankj.utilcode.util.EmptyUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jiangxiaofei.citycloud.R;
import com.jiangxiaofei.citycloud.adapter.FreshGoodAdapter;
import com.jiangxiaofei.citycloud.adapter.FreshRecyTypeAdapter;
import com.jiangxiaofei.citycloud.base.HaveNeedBaseActivity;
import com.jiangxiaofei.citycloud.bean.BannerBean;
import com.jiangxiaofei.citycloud.bean.FreshFruitCassifyBean;
import com.jiangxiaofei.citycloud.bean.GoodListBean;
import com.jiangxiaofei.citycloud.constants.BannerId;
import com.jiangxiaofei.citycloud.constants.Constants;
import com.jiangxiaofei.citycloud.constants.StringConstants;
import com.jiangxiaofei.citycloud.mvp.FreshConstant;
import com.jiangxiaofei.citycloud.mvp.FreshPersenter;
import com.jiangxiaofei.citycloud.mvp.MainConstant;
import com.jiangxiaofei.citycloud.mvp.MainPersenter;
import com.jiangxiaofei.citycloud.param.FleaGoodListParam;
import com.jiangxiaofei.citycloud.util.BannerViewHolder;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.zhouwei.mzbanner.MZBannerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * @author: JiangXFei
 * @date: 2017/11/27 0027
 * @content:商品列表
 */

public class FreshFruitGoodListActivity extends HaveNeedBaseActivity
        implements FreshConstant.GetFreshGoodListView, FreshConstant.GetFreshCassifyView,
        MainConstant.GetBannerView {
    @InjectView(R.id.recy_good_list)
    RecyclerView recyGoodList;
    @InjectView(R.id.tv_goods_date)
    TextView tvGoodsDate;
    @InjectView(R.id.tv_goods_instance)
    TextView tvGoodsInstance;
    @InjectView(R.id.tv_goods_type)
    TextView tvGoodsType;
    @InjectView(R.id.tv_goods_price)
    TextView tvGoodsPrice;
    @InjectView(R.id.srf_layout)
    SmartRefreshLayout srfLayout;
    @InjectView(R.id.banner)
    MZBannerView banner;

    private List<GoodListBean> goodListBeans = new ArrayList<>();
    private FreshGoodAdapter goodListAdapter;
    private FleaGoodListParam param;
    private int typeId;
    private int goodId;
    private String lat;
    private String lng;
    private int page = 1;
    private BDLocation bdLocation;
    private FreshConstant.GetFreshGoodListPersenter getFreshGoodListPersenter;
    private FreshConstant.GetFreshCassifyPersenter getFreshCassifyPersenter;
    private Intent intent;
    private PopupWindow mTypePopupWindow;
    private List<FreshFruitCassifyBean> fleaMenuType = new ArrayList<>();
    private FreshRecyTypeAdapter typeAdapter;
    private MainConstant.GetBannerPersenter getBannerPersenter;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_fresh_fruits_good_list_layout);
        ButterKnife.inject(this);
        initSearchTitle();
        initAdapter();
        getLocation();
        initRefres();
        intent = getIntent();
        typeId = intent.getIntExtra(StringConstants.FRESHGOODCASSIFYID, -1);
        goodId = intent.getIntExtra(StringConstants.FRESHGOODTYPEID, typeId);
        initParam();
        initPopup();
        getFreshGoodListPersenter = new FreshPersenter(mContext, (FreshConstant.GetFreshGoodListView) mActivity);
        getFreshGoodListPersenter.getGoodList();
        getFreshCassifyPersenter = new FreshPersenter(mContext, (FreshConstant.GetFreshCassifyView) mActivity);
        getFreshCassifyPersenter.getFreshCassify();
        getBannerPersenter = new MainPersenter(mContext, (MainConstant.GetBannerView) mActivity);
        getBannerPersenter.getBanner();
    }

    /**
     * 初始化刷新框架
     */
    private void initRefres() {
        srfLayout.setRefreshHeader(new MaterialHeader(this).setShowBezierWave(true));
        srfLayout.setRefreshFooter(new BallPulseFooter(this).setSpinnerStyle(SpinnerStyle.Scale));
        srfLayout.setOnLoadmoreListener(refreshlayout -> {
            refreshlayout.finishLoadmore(2000);
            page++;
            param.setPage(page);
            getFreshGoodListPersenter.getGoodList();
        });
        srfLayout.setOnRefreshListener(refreshlayout -> {
            refreshlayout.finishRefresh(2000);
            page = 1;
            param.setPage(page);
            goodListBeans.clear();
            getFreshGoodListPersenter.getGoodList();
        });
    }

    /**
     * 获取地理位置信息
     */
    private void getLocation() {
        bdLocation = Constants.bdLocation;
        if (EmptyUtils.isEmpty(bdLocation)) {
            Constants.bdclient.start();
        }
        bdLocation = Constants.bdLocation;
        lat = String.valueOf(bdLocation.getLatitude());
        lng = String.valueOf(bdLocation.getLongitude());
    }

    private void initAdapter() {
        goodListAdapter = new FreshGoodAdapter(R.layout.item_flea_good, goodListBeans);
        recyGoodList.setLayoutManager(new GridLayoutManager(mContext, 2));
        goodListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(mContext, FreshFruitsGoodDetailActivity.class);
                intent.putExtra(StringConstants.FRESHGOODID, Integer.valueOf(goodListBeans.get(position).getId()));
                startActivity(intent);
            }
        });
        recyGoodList.setAdapter(goodListAdapter);

    }

    //初始化请求参数
    private void initParam() {
        param = new FleaGoodListParam();
        param.setId(goodId);
        param.setLat(lat);
        param.setLng(lng);
        param.setPage(page);
        param.setDis(null);
        param.setIssue(null);
        param.setPrice(null);
    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {

    }

    @Override
    public void setData(Object o) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void message(String str) {

    }

    @Override
    public void getListOnSuccess(List<GoodListBean> bean) {
        if (page == 1) {
            goodListBeans.clear();
        }
        if (bean.size() > 0) {
            goodListBeans.addAll(bean);
        }
        goodListAdapter.notifyDataSetChanged();
    }

    @Override
    public FleaGoodListParam getGoodListParam() {
        return param;
    }

    /**
     * 设置按钮点击事件
     */
    @OnClick({R.id.tv_goods_date, R.id.tv_goods_instance, R.id.tv_goods_type, R.id.tv_goods_price})
    public void bOnClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.tv_goods_date:
                //发布时间
                setTextColor(tvGoodsDate);
                goodListBeans.clear();
                initParam();
                param.setIssue("0");
                getFreshGoodListPersenter.getGoodList();
                break;
            case R.id.tv_goods_instance:
                //距离
                setTextColor(tvGoodsInstance);
                goodListBeans.clear();
                initParam();
                param.setDis("0");
                getFreshGoodListPersenter.getGoodList();
                break;
            case R.id.tv_goods_type:
                // 类别
                setTextColor(tvGoodsType);
                goodListBeans.clear();
                mTypePopupWindow.showAsDropDown(tvGoodsType);
                initParam();
                param.setId(goodId);
                break;
            case R.id.tv_goods_price:
                //价格
                setTextColor(tvGoodsPrice);
                initParam();
                goodListBeans.clear();
                param.setPrice("0");
                getFreshGoodListPersenter.getGoodList();
                break;

        }
    }

    private void setTextColor(TextView textView) {
        tvGoodsDate.setTextColor(Color.parseColor("#7b7b7b"));
        tvGoodsInstance.setTextColor(Color.parseColor("#7b7b7b"));
        tvGoodsType.setTextColor(Color.parseColor("#7b7b7b"));
        tvGoodsPrice.setTextColor(Color.parseColor("#7b7b7b"));
        textView.setTextColor(Color.parseColor("#4abefb"));
    }

    /**
     * 初始化分类popupWindow
     */
    private void initPopup() {
        mTypePopupWindow = new PopupWindow(mContext);
        View view = LayoutInflater.from(mContext).inflate(R.layout.popup_flea_type_item_layout, null);
        mTypePopupWindow.setContentView(view);
        mTypePopupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        mTypePopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        mTypePopupWindow.setFocusable(true);
        mTypePopupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
        RecyclerView recyType = view.findViewById(R.id.recy_type_item);
        typeAdapter = new FreshRecyTypeAdapter(R.layout.item_flea_popup_text_layout, fleaMenuType);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        recyType.setLayoutManager(linearLayoutManager);
        typeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (int i = 0; i < fleaMenuType.size(); i++) {
                    fleaMenuType.get(i).setChecked(false);
                }
                fleaMenuType.get(position).setChecked(true);
                goodId = Integer.valueOf(fleaMenuType.get(position).getId());
                initParam();
                param.setId(goodId);
                getFreshGoodListPersenter.getGoodList();
                typeAdapter.notifyDataSetChanged();
                mTypePopupWindow.dismiss();
                goodListAdapter.notifyDataSetChanged();
            }
        });
        recyType.setAdapter(typeAdapter);
    }

    @Override
    public int getFreshCassifyId() {
        return goodId;
    }

    @Override
    public void getFreshCassifyOnSuccess(List<FreshFruitCassifyBean> cassifyBeans) {
        fleaMenuType.clear();
        if (cassifyBeans.size() > 0) {
            fleaMenuType.addAll(cassifyBeans);
        }
        typeAdapter.notifyDataSetChanged();
    }


    @Override
    public int getBannerParam() {
        switch (typeId) {
            default:
                break;
            case 1:
                return BannerId.FRESHGOODMENU1;
            case 2:
                return BannerId.FRESHGOODMENU2;
            case 3:
                return BannerId.FRESHGOODMENU3;
            case 4:
                return BannerId.FRESHGOODMENU4;
        }
        return 0;
    }

    @Override
    public void bannerSuccess(List<BannerBean> bannerBeans) {
        banner.setPages(bannerBeans, new BannerViewHolder(mContext));
    }
}
